package com.jixiantubu.wode;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.example.jixiantubu.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jixiantubu.adapter.Wo_fabutieAdapter;
import com.jixiantubu.bean.PostInfoBean;
import com.jixiantubu.util.MyApplication;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class WofabutieFragment extends Fragment {

	View wodetieView;
	ListView wo_tiefabu_lv;
	List<PostInfoBean> list = new ArrayList<PostInfoBean>();
	HttpUtils httpUtils = new HttpUtils();
	Wo_fabutieAdapter adapter;
	MyApplication application;
	private String username;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		username=getArguments().getString("username");
		wodetieView = inflater.inflate(R.layout.wo_tiezi_fabu_item, null);
		initViews();
		initData();
		return wodetieView;
	}

	private void initViews() {
		wo_tiefabu_lv = (ListView) wodetieView.findViewById(R.id.wo_tiefabu_lv);

	}

	private void initData() {
		adapter = new Wo_fabutieAdapter(getActivity(), list);
		wo_tiefabu_lv.setAdapter(adapter);
		getList(username);

	}

	private void getList(String username) {
		httpUtils = new HttpUtils();
		String url = "http://" + MyApplication.getIP()
				+ ":8080/Jinnang/Wo_Post_servlet";
		RequestParams requParams = new RequestParams();

		requParams.addBodyParameter("key", "1");
		requParams.addBodyParameter("username", username);

		httpUtils.send(HttpMethod.POST, url, requParams,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {

					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						String result = arg0.result;
						// shiyongGson框架进行json解析
						Gson gson = new Gson();
						Type typeOfT = new TypeToken<List<PostInfoBean>>() {
						}.getType();
						List<PostInfoBean> list1 = gson.fromJson(result,
								typeOfT);
						list.addAll(list1);
						Log.e("hua-eee-----", list.toString());
						adapter.notifyDataSetChanged();

					}
				});
	}
}
